﻿namespace Pub.Class
{
    using System;
    using System.Runtime.InteropServices;
    using System.Text;

    public class JsonSerialize : ISerialize
    {
        public T DecodeDeserialize<T>(string data, string key = "")
        {
            if (!key.IsNullEmpty())
            {
                return this.Deserialize<T>(data.DESDecode(key));
            }
            return this.Deserialize<T>(data);
        }

        public T Deserialize<T>(string data)
        {
            return data.FromJson<T>();
        }

        public T DeserializeFile<T>(string fileName)
        {
            string data = Log.ReadAll(fileName, Encoding.UTF8);
            return this.Deserialize<T>(data);
        }

        public string Serialize(object o)
        {
            return o.ToJson();
        }

        public string SerializeEncode(object o, string key = "")
        {
            if (!key.IsNullEmpty())
            {
                return this.Serialize(o).DESEncode(key);
            }
            return this.Serialize(o);
        }

        public void SerializeFile(object o, string fileName)
        {
            FileDirectory.FileDelete(fileName);
            Log.Write(fileName, this.Serialize(o));
        }
    }
}

